package com.chen.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chen.manager.mapper.OrderStatisticsMapper;
import com.chen.manager.service.OrderInfoService;
import com.chen.model.dto.order.OrderStatisticsDto;
import com.chen.model.entity.order.OrderStatistics;
import com.chen.model.vo.order.OrderStatisticsVo;
import com.chen.service.exception.MyException;
import org.apache.ibatis.ognl.ASTOr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author cgj
 * @date 3/23/25 9:04 AM
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;



    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        //返回的vo对象
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        List<OrderStatistics> orderStatisticsList=orderStatisticsMapper.getOrderStatisticsData(orderStatisticsDto);
        //null处理
        if(CollectionUtil.isEmpty(orderStatisticsList)){
            throw new MyException(229,"所选时间没有数据");
        }



//    //    普通的写法
//        ArrayList<String> dataList = new ArrayList<>();
//        ArrayList<BigDecimal> amountList = new ArrayList<>();
//        //  封装 OrderStatisticsVo
//        for (OrderStatistics orderStatistics : orderStatisticsList) {
//             //准备封装数据
//              dataList.add(DateUtil.format(orderStatistics.getOrderDate(),"yyyy-MM-dd"));
//              amountList.add(orderStatistics.getTotalAmount());
//        }
//        //封装返回的数据
//        orderStatisticsVo.setAmountList(amountList);
//        orderStatisticsVo.setDateList(dataList);

        //没有省略代码
//        List<Object> dateList = orderStatisticsList.stream().map(new Function<OrderStatistics, Object>() {
//            @Override
//            public Object apply(OrderStatistics orderStatistics) {
//                String date = DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd");
//                return date;
//            }
//        }).collect(Collectors.toList());

        //用 stream流的写发  map用于返回值类型不一样
//        List<String> dataList = orderStatisticsList.stream().map(orderStatistics -> {
//
//
//            return date;
//        }).collect(Collectors.toList());

        //如果就一行可以 省略 return
//        List<String> dataList = orderStatisticsList.stream().map(orderStatistics ->
//                DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")).collect(Collectors.toList());


        //直接把结果追加到vo中
        orderStatisticsVo.setDateList(orderStatisticsList.stream().map(orderStatistics ->
                DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")).collect(Collectors.toList()));

        //同理处理amount
//        List<BigDecimal> amount = orderStatisticsList.stream().map(orderStatistics -> {
//            BigDecimal totalAmount = orderStatistics.getTotalAmount();
//            return totalAmount;
//        }).collect(Collectors.toList());

        orderStatisticsVo.setAmountList(orderStatisticsList.stream().map(orderStatistics -> orderStatistics.getTotalAmount()).collect(Collectors.toList()));

        return orderStatisticsVo;
    }
}
